<template>
 <div style="padding:20px;padding-bottom:0px">
    <h1>{{id ? '编辑':'新建'}}铭文</h1>
    <el-alert
      v-if = "id?true:false"
      title="如果要更改铭文等级，请删除该铭文后重新添加！"
      type="warning"
      center
      show-icon>
    </el-alert>
    <el-form label-width = "120px" :rules = "rules" ref = "FormRef" :model = "model">
      <el-row>
        <el-col :span="6">
           <el-form-item label = "【铭文名称】" prop = "name">
            <el-input v-model="model.name" placeholder = "请输入铭文名称" style = "width:220px;"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span = "6">
           <el-form-item label = "【铭文等级】" prop = "f_id">
              <el-select v-model = "model.f_id" placeholder = "请选择铭文归属" :disabled = "id?true:false">
                <el-option v-for = "(item,i) in $store.state.inscs" :value = "item._id" :label = 'item.name' :key = "i"></el-option>
              </el-select>
            </el-form-item>
        </el-col>
        <el-col :span = "6">
          <el-form-item label = "【铭文图标】" prop = "icon">
            <el-upload
              class="avatar-uploader"
              accept = "image/jpg,image/jpeg,image/png"
              :before-upload="$handleBeforeUpload.handleBeforeUpload"
              :action="$upload"
              :show-file-list="false"
              :on-success="afterUpload">
              <img v-if="model.icon" :src="$upload+'/'+model.icon" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label = "【铭文属性】" class = "desc">
        <br>
        <h1 style="color:red">*如为百分比加成请直接填写，无需加上百分号</h1>
        <el-row :gutter="10">
          <el-col :span="5">【物理攻击】<el-input type = "number" v-model = "model.descript.Physical_attack"></el-input></el-col>
          <el-col :span="5">【攻击速度】<el-input type = "number" v-model = "model.descript.Attack_speed"></el-input></el-col>
          <el-col :span="5">【暴击效果】<el-input type = "number" v-model = "model.descript.Crit_effect"></el-input></el-col>
          <el-col :span="5">【法术攻击】<el-input type = "number" v-model = "model.descript.Spell_attack"></el-input></el-col>
        </el-row>
        <br>
        <el-row :gutter="10">
          <el-col :span="5">【最大法力】<el-input type = "number" v-model = "model.descript.Max_mana"></el-input></el-col>
          <el-col :span="5">【冷却缩减】<el-input type = "number" v-model = "model.descript.Cooldown_reduction"></el-input></el-col>
          <el-col :span="5">【最大生命】<el-input type = "number" v-model = "model.descript.Maximum_life"></el-input></el-col>
          <el-col :span="5">【法术防御】<el-input type = "number" v-model = "model.descript.Spell_defense"></el-input></el-col>
        </el-row>
        <br>
        <el-row :gutter="10">
          <el-col :span="5">【移动速度】<el-input type = "number" v-model = "model.descript.Moving_speed"></el-input></el-col>
          <el-col :span="5">【物理穿透】<el-input type = "number" v-model = "model.descript.Physical_penetration"></el-input></el-col>
          <el-col :span="5">【暴击几率】<el-input type = "number" v-model = "model.descript.Crit_chance"></el-input></el-col>
          <el-col :span="5">【物理吸血】<el-input type = "number" v-model = "model.descript.Physical_vampire"></el-input></el-col>
        </el-row>
        <br>
        <el-row :gutter="10">
          <el-col :span="5">【法术穿透】<el-input type = "number" v-model = "model.descript.Spell_penetration"></el-input></el-col>
          <el-col :span="5">【每五秒回蓝】<el-input type = "number" v-model = "model.descript.Blue_every_five_seconds"></el-input></el-col>
          <el-col :span="5">【法术吸血】<el-input type = "number" v-model = "model.descript.Spell_vampire"></el-input></el-col>
          <el-col :span="5">【物理防御】<el-input type = "number" v-model = "model.descript.Physical_defense"></el-input></el-col>
        </el-row>
        <br>
        <el-row :gutter="10">
          <el-col :span="5">【每五秒回血】<el-input type = "number" v-model = "model.descript.Healing_every_five_seconds"></el-input></el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button type = 'primary' @click="save">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  props: ['id'],
  data () {
    return {
      model: {
        f_id: '',
        name: '',
        icon: '',
        descript: {
          Physical_attack: 0,
          Attack_speed: 0,
          Physical_vampire: 0,
          Spell_penetration: 0,
          Maximum_life: 0,
          Spell_defense: 0,
          Physical_penetration: 0,
          Crit_chance: 0,
          Spell_attack: 0,
          Spell_vampire: 0,
          Physical_defense: 0,
          Healing_every_five_seconds: 0,
          Moving_speed: 0,
          Cooldown_reduction: 0,
          Crit_effect: 0,
          Max_mana: 0,
          Blue_every_five_seconds: 0
        }
      },
      rules: {
        name: {
          required: true,
          message: '请填写铭文名称',
          trigger: 'blur'
        },
        icon: {
          required: true,
          message: '请上传铭文图片',
          trigger: 'blur'
        },
        f_id: {
          required: true,
          message: '请选择铭文等级',
          trigger: 'blur'
        }
      }
    }
  },
  methods: {
    save () {
      for (const index in this.model.descript) {
        if (this.model.descript[index] === '') {
          this.model.descript[index] = 0
        }
      }
      this.$refs.FormRef.validate(async (valid) => {
        if (!valid) {
          return this.$message.warning('请正确填写所有相关信息')
        }
        const res = this.id ? await this.$http.put('rest/Insc', { _id: this.model.f_id, index: this.id, children: this.model }) : await this.$http.post('rest/Insc/add', { _id: this.model.f_id, model: this.model })
        if (res.status !== 200) {
          return this.$message.error(this.id ? '修改铭文失败' : '添加铭文失败')
        }
        this.$message.success(this.id ? '修改铭文成功' : '添加铭文成功')
        this.$store.commit('UpdataInsc', res.data)
        window.sessionStorage.setItem('ActivityName', this.model.f_id)
        window.sessionStorage.setItem('activePath', '/inscription/list')
        this.$router.push({ path: '/inscription/list' })
      })
    },
    afterUpload (res) {
      this.model.icon = res.url
    },
    async fetch () {
      await this.$http.get(`rest/Insc/${this.id}`).then(res => {
        if (res.status !== 200) {
          window.sessionStorage.setItem('activePath', '/inscription/list')
          this.$router.push({ path: '/inscription/list' })
          return this.$message.error('获取铭文信息失败')
        }
        this.model = res.data
      })
    }
  },
  created () {
    this.id && this.fetch()
  },
  watch: {
    '$route.path': function (newVal) {
      if (newVal === '/inscription/create') {
        this.$data.model = { ...this.$options.data().model }
      }
    }
  }
}
</script>
<style scoped lang="less">
.desc {
  .el-input{
    width: 150px;
  }
}
.el-alert{
  margin-bottom: 10px;
}
</style>
